package com.baosight.sczx.zm.cn.service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cn.CoalCode;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.zm.cn.domain.ZMCNCcy;
/**
 * 内部抽查样数据
 */
public class ServiceZMCN41 extends ServiceEPBase {
	/**
	 * <p>Function: initLoad</p>
	 * <p>Description: 初始化</p> 
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		inInfo.addBlock(EiConstant.resultBlock).set(EiConstant.limitStr, "50");//设置每页默认显示记录数
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
	/**
	 * <p>Function: query</p>
	 * <p>Description: 查询</p> 
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		String fdYear=inInfo.getString("year");
		String fdMonth=inInfo.getString("month");
//		String fdReportDepartmentId=inInfo.getString("fdReportDepartmentId");
		if(fdYear!=null && !fdYear.trim().equals("")){
			inInfo.setCell("inqu_status", 0, "fdYear", fdYear);
		}
		if(fdMonth!=null && !fdMonth.trim().equals("")){
			inInfo.setCell("inqu_status", 0, "fdMonth", fdMonth);
		}
		inInfo.setCell("inqu_status", 0, "fdReportDepartmentId",  UserSession.getInSessionProperty("orgCode"));
		inInfo.setCell("inqu_status", 0, "code_type", "内部抽查样");//用来区分过滤内部抽查样和外部抽查样
		EiInfo outInfo = super.query(inInfo, "ZMCNCcy.query", new ZMCNCcy());
		outInfo.addBlock(StaticBlocks.getBlockData("cmes.ch.status", new String[] { "草拟", "上报", "已发布" }, new String[] { "草拟", "上报", "已发布" }, false));// 状态
		String mzCode = InitialData.getInstance().getEiBlockByNo("cmes.cn.meizhiCode").getCellStr(0, "display");
		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", false,mzCode));
//		outInfo.set("fdCreatorId", UserSession.getUserId());//登录人的默认值
//		outInfo.set("fdCreator", UserSession.getInSessionProperty("UserCname"));
//		outInfo.set("fdCreateDepartmentId", UserSession.getInSessionProperty("orgCode"));//单位的默认值		
//		outInfo.set("fdCreateDepartment", UserSession.getInSessionProperty("orgName"));//单位的默认值
		outInfo.addBlock(CoalCode.getInstance().getCoalCodeBlock("cmes.cn.coalcode","fdMyDetail","fdMyCode", null, null));//煤样编码
		return outInfo;
	}	

	/**
	 * <p>Function: insert</p>
	 * <p>Description: 新增</p> 
	 */
	@Override	
	public EiInfo insert(EiInfo inInfo) {
		EiInfo outinfo = new EiInfo();
		ZMCNCcy _dom = new ZMCNCcy();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
//        String fdCreatorId=(String)UserSession.getUserId();//登录人的默认值);
        String fdCreator=(String)UserSession.getInSessionProperty("UserCname");
        String fdCreateDepartmentId=(String)UserSession.getInSessionProperty("orgCode");//单位的默认值		
        String fdCreateDepartment=(String)UserSession.getInSessionProperty("orgName");//单位的默认值
		String fdYear=inInfo.getString("year");
		if(fdYear==null||fdYear.trim().equals("")){
			fdYear=inInfo.getCellStr("inqu_status", 0, "fdYear");
		}
		String fdMonth=inInfo.getString("month");
		if(fdMonth==null||fdMonth.trim().equals("")){
			fdMonth=inInfo.getCellStr("inqu_status", 0, "fdMonth");
		}
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CN_CCY"));
			_dom.setFdReportPerson(fdCreator);
			_dom.setFdReportDepartment(fdCreateDepartment);
			_dom.setFdReportDepartmentId(fdCreateDepartmentId);
			_dom.setFdYear(fdYear);
			_dom.setFdMonth(fdMonth);
			this.dao.insert("ZMCNCcy.insert", _dom);
		}
		outinfo = this.query(inInfo);
		outinfo.setStatus(1);
		outinfo.setMsg("操作" + rowcount + "记录成功");
		return outinfo;
	}
	 
	/**
	 * <p>Function: update</p>
	 * <p>Description: 修改</p> 
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo outInfo = new EiInfo();
		ZMCNCcy _dom = new ZMCNCcy();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
//      String fdCreatorId=(String)UserSession.getUserId();//登录人的默认值);
	    String fdCreator=(String)UserSession.getInSessionProperty("UserCname");
	    String fdCreateDepartmentId=(String)UserSession.getInSessionProperty("orgCode");//单位的默认值		
	    String fdCreateDepartment=(String)UserSession.getInSessionProperty("orgName");//单位的默认值

		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			_dom.setFdReportPerson(fdCreator);
			_dom.setFdReportDepartment(fdCreateDepartment);
			_dom.setFdReportDepartmentId(fdCreateDepartmentId);
			this.dao.update("ZMCNCcy.update", _dom);
		}
		outInfo = this.query(inInfo);
		outInfo.setStatus(1);
		outInfo.setMsg("操作" + rowcount + "记录成功");
		return outInfo;
	}	
	/**
	 * <p>Function: delete</p>
	 * <p>Description: 删除</p> 
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo outInfo = new EiInfo();
		ZMCNCcy _dom = new ZMCNCcy();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			this.dao.delete("ZMCNCcy.delete", _dom);
		}
		outInfo = this.query(inInfo);
		outInfo.setStatus(1);
		outInfo.setMsg("操作" + rowcount + "记录成功");
		return outInfo;
	}	
	/**
	 * <p>Function: report</p>
	 * <p>Description: 上报</p> 
	 */
	public EiInfo report(EiInfo inInfo) {
		//生效的时候置汇报时间为当前时间
		String reportdate=StaticTools.getDate();
		ZMCNCcy _dom = new ZMCNCcy();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		for (int i = 0; i < rowcount; i++) {
			eiBlock.setCell(i, "fdReportTime", reportdate);
		}
		super.update(inInfo, "ZMCNCcy.report");
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
	
	/**
	 * 校验是否可以操作
	 * <p>Function: validate</p>
	 * <p>Description: 重复性校验</p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-7-20 09:46:16 李红星 create
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	public EiInfo validate(EiInfo inInfo) {
		ZMCNCcy _dom = new ZMCNCcy();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		String oper=(String)inInfo.get("efCurButtonEname");
		for (int i = 0; i < rowcount; i++) {
			Map<String, String> params = new HashMap<String, String>();
			_dom.fromMap(eiBlock.getRow(i));
			params.put("fdMyNum", _dom.getFdMyNum());
			if(oper.equalsIgnoreCase("update1")||oper.equalsIgnoreCase("update"))
			{
				params.put("fdKeyId", _dom.getFdKeyId());
			}
			List result = dao.query("ZMCNCcy.validate", params);
			if(result != null && result.size() > 0) {
				inInfo.setStatus(-1);
				StringBuffer msg = new StringBuffer("提交失败，");
				msg.append("原煤样编号重复");
				
				inInfo.setMsg(msg.toString());
				return inInfo;
			}
		}
		inInfo.setStatus(1);
		return inInfo;
	}
} 